package view.backing.Utils.Validator;

import chemtools_logica.Beans.UsuarioBean;

import chemtools_logica.LN.IR.LN_SFEventoRemote;

import java.util.Hashtable;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;

import javax.faces.validator.ValidatorException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import javax.servlet.http.HttpSession;

public class UsuarioSessionValidator implements Validator{
    
    
    
    @Override 
    public void validate(FacesContext facesContext, UIComponent uiComponent, Object object) {
        int idUsuario = Integer.parseInt(object.toString());
        
        HttpSession sesion = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        UsuarioBean usuario = (UsuarioBean) sesion.getAttribute("usuario");
        
        if(idUsuario == usuario.getIdUsuario()){
            FacesMessage fm = new FacesMessage("El usuario no puede registrar un evento propio");
            throw new ValidatorException(fm);
        }
        
        
            
    }
    private static Context getInitialContext() throws NamingException {
     Hashtable env = new Hashtable();
     // WebLogic Server 10.x/12.x connection details
     env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
     env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
     return new InitialContext(env);
    }
    
}

       